/**
 * 
 */
package com.auth.action;

import java.util.List;

import org.apache.log4j.Logger;


import com.action.AbstractAction;
import com.auth.model.CpAccount;
import com.auth.service.CpAccountService;
import com.auth.service.CpAccountServiceImpl;
import com.manage.cache.ChannelManager;
import com.manage.cache.CpInfoManager;
import com.manage.model.Channel;
import com.manage.model.CpInfo;


public class CpAccountAction extends AbstractAction
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger logger;
	private CpAccountService service;
	
	public CpAccountAction()
	{
		logger = Logger.getLogger(this.getClass());
		service = new CpAccountServiceImpl();
	}
	
	public String execute()
	{
		return SUCCESS;
	}

	private List<CpAccount> cpaccounts;
	private CpAccount       cpaccount;
	private String        type;
	private List<CpInfo> cpInfos;
	private List<Channel> channels;
	
	public String list()
	{
		logger.debug("list account");
		cpaccounts = service.getCpAccounts();
		cpInfos = CpInfoManager.getInstance().getCpInfos();
		channels = ChannelManager.getInstance().getChannels();
		return SUCCESS;
	}
	
	public String show()
	{
		if (cpaccount.getId() != null && cpaccount.getId().length() > 0)
		{
			cpaccount = service.getCpAccount(cpaccount.getId());
			type = "update";
		}
		else
		{
			cpaccount.setId("");
			cpaccount.setPassword(com.utils.security.PasswordGenerator.generate(8));
			type = "insert";
		}

		channels = ChannelManager.getInstance().getChannels();
		cpInfos = CpInfoManager.getInstance().getCpInfos();
		return SUCCESS;
	}
	
	public String save()
	{
		boolean f = true;
		if(type.equals("insert"))
		{
			CpAccount ca = service.getCpAccount(cpaccount.getId());
			if(ca!=null && ca.getId().equals(cpaccount.getId()))
			{
				addActionError("渠道用户名不能重复");
				f = false;
			}
		}
		if(f)
		{
			service.saveCpAccount(operator, type, cpaccount);
		}
		return SUCCESS;
	}
	
	private String oldPassword;
	private String newPassword;
	public String change()
	{
		if (operator == null)
		{
			addActionError("请重新登录");
			return INPUT;
		}
		cpaccount = service.getCpAccount(cpaccount.getId());
		return SUCCESS;
	}
	
	public String password_save()
	{
		if (operator == null)
		{
			addActionError("请重新登录");
			return INPUT;
		}

		CpAccount act = service.getCpAccount(cpaccount.getId());
		act.setPassword(newPassword);
		service.updatePassword(operator, cpaccount.getId(), newPassword);
		return SUCCESS;
	}

	public List<CpAccount> getCpaccounts() {
		return cpaccounts;
	}

	public void setCpaccounts(List<CpAccount> cpaccounts) {
		this.cpaccounts = cpaccounts;
	}

	public CpAccount getCpaccount() {
		return cpaccount;
	}

	public void setCpaccount(CpAccount cpaccount) {
		this.cpaccount = cpaccount;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<CpInfo> getCpInfos() {
		return cpInfos;
	}

	public void setCpInfos(List<CpInfo> cpInfos) {
		this.cpInfos = cpInfos;
	}

	public List<Channel> getChannels() {
		return channels;
	}

	public void setChannels(List<Channel> channels) {
		this.channels = channels;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	
	
}
